﻿Imports System.Data.SqlClient
Imports System.Configuration
Imports System.Data
Imports System.Net.Mail

Partial Class Account_ForgotPassword
    Inherits System.Web.UI.Page

    Protected Sub btnSbmit_Click(sender As Object, e As EventArgs) Handles btnSbmit.Click
        'Create Connection String And SQL Statement
        Dim connectionString As String = System.Configuration.ConfigurationManager.AppSettings("ConnectionString")
        Dim strSelect As String = "SELECT UserName,Password FROM [User] WHERE Email = @Email"

        Dim connection As New SqlConnection(connectionString)
        Dim command As New SqlCommand()
        command.Connection = connection
        command.CommandType = CommandType.Text
        command.CommandText = strSelect

        Dim email As New SqlParameter("@Email", SqlDbType.NChar, 50)
        email.Value = txtForgotPass.Text.Trim().ToString()
        command.Parameters.Add(email)

        'Create Dataset to store results and DataAdapter to fill Dataset
        Dim dsPwd As New DataSet()
        Dim dAdapter As New SqlDataAdapter(command)
        connection.Open()
        dAdapter.Fill(dsPwd)
        connection.Close()
        If dsPwd.Tables(0).Rows.Count > 0 Then
            Dim loginInfo As New MailMessage()
            loginInfo.[To].Add(txtForgotPass.Text.ToString())
            loginInfo.From = New MailAddress("sergios89@gmail.com")
            loginInfo.Subject = "Forgot Password Information"

            loginInfo.Body = "Username: " & Convert.ToString(dsPwd.Tables(0).Rows(0)("UserName")) & "<br><br>Password: " & Convert.ToString(dsPwd.Tables(0).Rows(0)("Password")) & "<br><br>"
            loginInfo.IsBodyHtml = True
            Dim smtp As New SmtpClient()
            smtp.Host = "smtp.gmail.com"
            smtp.Port = 587
            smtp.EnableSsl = True
            smtp.Credentials = New System.Net.NetworkCredential("sergios89@gmail.com", "$ergio$")
            smtp.Send(loginInfo)
            lblMessage.Text = "Password is sent to your email id,you can now login"
        Else
            lblMessage.Text = "Email Address Not Registered"
        End If

    End Sub

End Class
